﻿using FineUI;
using SysManage.BLL;
using System;
using ZHDJ.Core.Util;
using ZHDJ.Data.Models.Common;
using ZHDJ.Data.Models.Grassroots;
using ZHDJ.WebPage;

namespace SysManage.Module.Grassroots
{
    public partial class MassesVoiceEdit : PageBase
    {
        #region Page_Load
        /// <summary>
        /// 本页面的浏览权限，空字符串表示本页面不受权限控制
        /// </summary>
        public override string ViewPower
        {
            get
            {
                return "GMassesVoiceAdd";
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
                LoadData();
            }
        }

        private void BindData()
        {
            ControlBusiness.ControlBind(ddlGroup, PartyGrassrootsGroup.GetGroupData("", IsSystemAdmin ? "" : this.BranchID), "GroupName", "ID", true);
            ControlBusiness.ControlBindDataDictionary(ddlMessageWay, "ZHDJ.Grassroots.MessageWay", true);
            ControlBusiness.ControlBindDataDictionary(ddlMassComment, "ZHDJ.Grassroots.MassComment", true);
        }

        private void LoadData()
        {
            btnClose.OnClientClick = ActiveWindow.GetHideReference();
            string id = GetQueryIntValue("id");
            if (id != "")
            {
                PartyGrassrootsSuggestion oModel = DB.PartyGrassrootsSuggestions.Find(id);
                if (oModel == null)
                {
                    // 参数错误，首先弹出Alert对话框然后关闭弹出窗口
                    Alert.Show("参数错误！", String.Empty, ActiveWindow.GetHideReference());
                    return;
                }

                FormRow1.Hidden = FormRow2.Hidden = FormRow3.Hidden = false;
                ddlGroup.Readonly = true;
                ddlItem.Readonly = true;
                txtTitle.Readonly = txtUserName.Readonly = txtMobileNo.Readonly = txtContent.Readonly = true;

                this.ddlGroup.SelectedValue = PartyGrassrootsGroupItem.QueryGroupIDByItemID(oModel.ItemID);
                ControlBusiness.ControlBind(ddlItem, PartyGrassrootsGroupItem.QueryItemDataByGroupID(ddlGroup.SelectedValue, IsSystemAdmin ? "" : this.BranchID), "ItemName", "ItemID");
                ddlItem.SelectedValue = oModel.ItemID;
                txtTitle.Text = oModel.Title;
                txtUserName.Text = oModel.UserName;
                txtMobileNo.Text = oModel.MobileNo;
                dtCollectionTime.Text = Convert.ToDateTime(oModel.CollectionTime).ToString("yyyy-MM-dd");
                txtContent.Text = oModel.Content;
                ddlMassComment.SelectedValue = oModel.MassComment;
                ddlMessageWay.SelectedValue = oModel.HandlerBy;
                txtGrassrootsAdmin.Text = oModel.Receiver;
                txtReply.Text = oModel.Reply;
                dpReplyTime.Text = oModel.ModifyTime == null ? DateTime.Now.ToString("yyyy-MM-dd") : Convert.ToDateTime(oModel.ModifyTime).ToString("yyyy-MM-dd");

                if (oModel.BranchID == "")
                {
                    ppBranch.Value = "";
                    ppBranch.Text = "";
                }
                else
                {
                    ppBranch.Value = oModel.BranchID;
                    ppBranch.Text = ControlBusiness.GetBranchPickerText(oModel.BranchID);
                }
                if (this.CurrentUser.LoginID == "admin")
                {
                    ppBranch.Enabled = true;
                }
                else
                {
                    ppBranch.Enabled = false;
                    txtGrassrootsAdmin.Required = true;
                    txtGrassrootsAdmin.ShowRedStar = true;
                    txtGrassrootsAdmin.RequiredMessage = "清填写驻点接待人员";
                    txtReply.Required = true;
                    txtReply.ShowRedStar = true;
                    txtReply.RequiredMessage = "清填写办理情况";
                }


            }
            else
            {
                dtCollectionTime.Text = DateTime.Now.ToString("yyyy-MM-dd");
            }
        }
        #endregion




        #region Events

        private void SaveData()
        {
            string id = GetQueryIntValue("id");
            PartyGrassrootsSuggestion oModel;
            if (id != "")
            {
                //更新
                oModel = DB.PartyGrassrootsSuggestions.Find(id);
                if (oModel == null)
                {
                    Alert.Show("参数错误！", String.Empty, ActiveWindow.GetHideReference());
                    return;
                }


                oModel.ModifyTime = Convert.ToDateTime(dpReplyTime.Text);
                oModel.ModifyBy = CurrentUser.LoginID;

                DB.SaveChanges();
            }
            else
            {
                oModel = new PartyGrassrootsSuggestion();
                oModel.ID = StringHelper.NewID;
                oModel.ItemID = ddlItem.SelectedValue;

                oModel.CreateTime = DateTime.Now;
                oModel.CreateBy = CurrentUser.LoginID;

                DB.PartyGrassrootsSuggestions.Add(oModel);


            }
            oModel.Title = txtTitle.Text.Trim();
            oModel.UserName = txtUserName.Text.Trim();
            oModel.MobileNo = txtMobileNo.Text.Trim();
            oModel.Content = txtContent.Text.Trim();
            oModel.MassComment = ddlMassComment.SelectedValue;
            oModel.HandlerBy = ddlMessageWay.SelectedValue;
            oModel.CollectionTime = Convert.ToDateTime(dtCollectionTime.Text);
            oModel.Status = 4;
            oModel.Receiver = txtGrassrootsAdmin.Text.Trim();
            oModel.Reply = txtReply.Text.Trim();
            oModel.Status = string.IsNullOrEmpty(oModel.Reply.Trim()) ? 1 : 4;
            if (txtReply.Text == "")
            {
                oModel.Status = 1;
            }

            oModel.BranchID = ppBranch.Value;
            DB.SaveChanges();
        }

        /// <summary>
        /// 保存并关闭按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSaveClose_Click(object sender, EventArgs e)
        {
            SaveData();

            PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
        }

        protected void ddlGroup_SelectedIndexChanged(object sender, EventArgs e)
        {
            ControlBusiness.ControlBind(ddlItem, PartyGrassrootsGroupItem.QueryItemDataByGroupID(ddlGroup.SelectedValue, IsSystemAdmin ? "" : this.BranchID), "ItemName", "ID", true);
        }
        #endregion
    }
}